
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta charset="utf-8"></meta>
      <meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
      <title>Chapter&nbsp;7.&nbsp;Export / Import - APOC User Guide 3.5</title>
      <link rel="stylesheet" type="text/css" href="../docbook.css"></link>
      <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans:400,300,400italic,600,300italic"></link>
      <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/codemirror.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/theme/neo.min.css"></link>
      <link rel="stylesheet" type="text/css" href="../css/chunked-base.css"></link>
      <link rel="stylesheet" type="text/css" href="../css/extra.css"></link><script src="//code.jquery.com/jquery-1.12.4.js" type="text/javascript"></script><script src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js" type="text/javascript"></script><script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" type="text/javascript"></script><script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/codemirror.min.js" type="text/javascript"></script><script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/addon/runmode/runmode.min.js" type="text/javascript"></script><script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/mode/cypher/cypher.min.js" type="text/javascript"></script><script src="../javascript/datatable.js" type="text/javascript"></script><script src="../javascript/colorize.js" type="text/javascript"></script><script src="../javascript/tabs-for-chunked.js" type="text/javascript"></script><script src="../javascript/mp-nav.js" type="text/javascript"></script><script src="../javascript/versionswitcher.js" type="text/javascript"></script><script src="../javascript/version.js" type="text/javascript"></script><script src="//s3-eu-west-1.amazonaws.com/alpha.neohq.net/docs/new-manual/assets/search.js" type="text/javascript"></script><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"></meta>
      <link rel="prev" href="../schema/meta-graph/" title="9.1.&nbsp;Meta Graph"></link>
      <link rel="next" href="load-json/" title="7.2.&nbsp;Load JSON"></link>
      <link rel="shortcut icon" href="https://neo4j.com/wp-content/themes/neo4jweb/favicon.ico"></link><script>
        $(document).ready(function() {
          CodeMirror.colorize();
          tabTheSource($('body'));
          var $header = $('header').first();
          $header.prepend(
            $('<a href="" id="logo"><img src="https://neo4j.com/wp-content/themes/neo4jweb/assets/images/neo4j-logo-2015.png" alt="Neo4j Logo"></img></a>')
          );
          var $sidebar = $('<div id="sidebar-wrapper"></div>');
          $.get('toc.html', function (d){
            $(d).appendTo($sidebar);
            highlightToc();
            highlightLibraryHeader();
          });
          $sidebar.insertAfter($('header').first());
        });
        </script></head>
   <body>
      <header>
         <div class="searchbox">
            <form id="search-form" class="search" name="search-form" role="search"><input id="search-form-input" name="q" title="search" type="search" lang="en" placeholder="Search Neo4j docs..." aria-label="Search Neo4j documentation" max-length="128" required="required"></input><input id="search-form-button" type="submit" value="Search"></input></form>
         </div>
         <ul class="documentation-library">
            <li><a href="https://neo4j.com/docs/operations-manual/current">Operations Manual</a></li>
            <li><a href="https://neo4j.com/docs/developer-manual/current/">Developer Manual</a></li>
            <li><a href="https://neo4j.com/docs/ogm-manual/current/">OGM Manual</a></li>
            <li><a href="https://neo4j.com/docs/graph-algorithms/current/">Graph Algorithms</a></li>
            <li><a href="https://neo4j-contrib.github.io/neo4j-apoc-procedures/3.5/">APOC</a></li>
            <li><a href="https://neo4j.com/docs/java-reference/current/">Java Reference</a></li>
         </ul>
         <nav id="header-nav"><span class="nav-previous"><a accesskey="p" href="../schema/meta-graph/"><span class="fa fa-long-arrow-left" aria-hidden="true"></span>Meta Graph</a></span><span class="nav-current">
               <p class="nav-title hidden">Chapter&nbsp;7.&nbsp;Export / Import</p></span><span class="nav-next"><a accesskey="n" href="load-json/">Load JSON<span class="fa fa-long-arrow-right" aria-hidden="true"></span></a></span></nav>
      </header>
      <div id="search-results" class="hidden"></div>
      <section class="chapter" id="export-import">
         <div class="titlepage">
            <div>
               <div>
                  <h1 class="title">Chapter&nbsp;7.&nbsp;Export / Import</h1>
               </div>
            </div>
         </div>
         <div class="admonitionblock note">
            <table>
               <tbody>
                  <tr>
                     <td class="icon"><i class="fa icon-note" title="note"></i></td>
                     <td class="content">
                        <p>In case you have the default configuration with <code class="literal">apoc.import.file.use_neo4j_config=true</code> the export consider as root the directory defined into the <code class="literal">dbms.directories.import</code> property
                        </p>
                     </td>
                  </tr>
               </tbody>
            </table>
         </div>
         <section class="section" id="_loading_data_from_web_apis">
            <div class="titlepage">
               <div>
                  <div>
                     <h2 class="title" style="clear: both"><a class="anchor" href="#_loading_data_from_web_apis"></a>7.1.&nbsp;Loading Data from Web-APIs
                     </h2>
                  </div>
               </div>
            </div>
            <p>Supported protocols are <code class="literal">file</code>, <code class="literal">http</code>, <code class="literal">https</code>, <code class="literal">s3</code>, <code class="literal">hdfs</code> with redirect allowed.
            </p>
            <p>In case no protocol is passed, this procedure set will try to check whether the url is actually a file.</p>
            <div class="admonitionblock note">
               <table>
                  <tbody>
                     <tr>
                        <td class="icon"><i class="fa icon-note" title="note"></i></td>
                        <td class="content">
                           <p>As <code class="literal">apoc.import.file.use_neo4j_config</code> is enabled, the procedures check whether file system access is allowed and possibly constrained to a specific directory by
                              reading the two configuration parameters <code class="literal">dbms.security.allow_csv_import_from_file_urls</code> and <code class="literal">dbms.directories.import</code> respectively.
                              If you want to remove these constraints please set <code class="literal">apoc.import.file.use_neo4j_config=false</code></p>
                        </td>
                     </tr>
                  </tbody>
               </table>
            </div>
            <div class="informaltable">
               <div class="table" id="d0e5885">
                  <table class="informaltable" border="1">
                     <colgroup>
                        <col class="col_1"></col>
                        <col class="col_2"></col>
                     </colgroup>
                     <tbody>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">CALL apoc.load.json('http://example.com/map.json', [path], [config]) YIELD value as person CREATE (p:Person) SET p = person</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>load from JSON URL (e.g. web-api) to import JSON as stream of values if the JSON was an array or a single value if it was
                                 a map
                              </p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">CALL apoc.load.xml('http://example.com/test.xml', ['xPath'], [config]) YIELD value as doc CREATE (p:Person) SET p.name = doc.name</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>load from XML URL (e.g. web-api) to import XML as single nested map with attributes and <code class="literal">_type</code>, <code class="literal">_text</code> and <code class="literal">_children</code> fields.
                              </p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">CALL apoc.load.xmlSimple('http://example.com/test.xml') YIELD value as doc CREATE (p:Person) SET p.name = doc.name</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>load from XML URL (e.g. web-api) to import XML as single nested map with attributes and <code class="literal">_type</code>, <code class="literal">_text</code> fields and <code class="literal">_&lt;childtype&gt;</code> collections per child-element-type.
                              </p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">CALL apoc.load.csv('url',{sep:";"}) YIELD lineNo, list, strings, map, stringMap</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>load CSV fom URL as stream of values
                                 config contains any of: <code class="literal">{skip:1,limit:5,header:false,sep:'TAB',ignore:['aColumn'],arraySep:';',results:['map','list','strings','stringMap'],
                                    nullValues:[''],mapping:{years:{type:'int',arraySep:'-',array:false,name:'age',ignore:false,nullValues:['n.A.']}}</code></p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">CALL apoc.load.xls('url','Sheet'/'Sheet!A2:B5',{config}) YIELD lineNo, list, map</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>load XLS fom URL as stream of values
                                 config contains any of: <code class="literal">{skip:1,limit:5,header:false,ignore:['aColumn'],arraySep:';'+
                                    nullValues:[''],mapping:{years:{type:'int',arraySep:'-',array:false,name:'age',ignore:false,nullValues:['n.A.']}}</code></p>
                           </td>
                        </tr>
                     </tbody>
                  </table>
               </div>
            </div>
         </section>
         <section class="section" id="export-csv">
            <div class="titlepage">
               <div>
                  <div>
                     <h2 class="title" style="clear: both"><a class="anchor" href="#export-csv"></a>7.7.&nbsp;Export to CSV
                     </h2>
                  </div>
               </div>
            </div>
            <p><code class="literal">YIELD file, source, format, nodes, relationships, properties, time, rows, data</code></p>
            <div class="informaltable">
               <div class="table" id="d0e7874">
                  <table class="informaltable" border="1">
                     <colgroup>
                        <col class="col_1"></col>
                        <col class="col_2"></col>
                     </colgroup>
                     <tbody>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">apoc.export.csv.query(query,file,config)</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>exports results from the Cypher statement as CSV to the provided file</p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">apoc.export.csv.all(file,config)</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>exports whole database as CSV to the provided file</p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">apoc.export.csv.data(nodes,rels,file,config)</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>exports given nodes and relationships as CSV to the provided file</p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">apoc.export.csv.graph(graph,file,config)</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>exports given graph object as CSV to the provided file</p>
                           </td>
                        </tr>
                     </tbody>
                  </table>
               </div>
            </div>
            <p>If the file name is passed as <code class="literal">null</code> and the config <code class="literal">stream:true</code> the results are streamed back in the <code class="literal">data</code> column, e.g.
            </p>
            <section class="section" id="_note_2">
               <div class="titlepage">
                  <div>
                     <div>
                        <h3 class="title"><a class="anchor" href="#_note_2"></a>7.7.1.&nbsp;Note:
                        </h3>
                     </div>
                  </div>
               </div>
               <p>For <code class="literal">apoc.export.csv.all/data/graph</code> nodes and relationships properties are ordered alphabetically, following this general structure:
               </p>
               <p><code class="literal">_id,_labels,&lt;list_nodes_properties_naturally_sorted&gt;,_start,_end,_type,&lt;list_rel_properties_naturally_sorted&gt;</code>, so for instance:
               </p>
               <p><code class="literal">_id,_labels,age,city,kids,male,name,street,_start,_end,_type,bar,foo</code></p>
               <p>The labels exported are ordered alphabetically.
                  The output of <code class="literal">labels()</code> function is not sorted, use it in combination with <code class="literal">apoc.coll.sort()</code>.
               </p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.export.csv.all(null, {stream:true,batchSize:100}) YIELD data RETURN data</code></pre></section>
         </section>
         <section class="section" id="export-json">
            <div class="titlepage">
               <div>
                  <div>
                     <h2 class="title" style="clear: both"><a class="anchor" href="#export-json"></a>7.8.&nbsp;Export to Json File
                     </h2>
                  </div>
               </div>
            </div>
            <p>Make sure to set the config options in your <code class="literal">neo4j.conf</code></p>
            <p><span class="formalpara-title">Neo4j.conf.&nbsp;</span>
               
            </p><pre class="screen highlight"><code>apoc.export.file.enabled=true</code></pre><p>
               
            </p>
            <p><code class="literal">YIELD file, source, format, nodes, relationships, properties, time, rows</code></p>
            <div class="informaltable">
               <div class="table" id="d0e7965">
                  <table class="informaltable" border="1">
                     <colgroup>
                        <col class="col_1"></col>
                        <col class="col_2"></col>
                     </colgroup>
                     <tbody>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">apoc.export.json.query(query,file,config)</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>exports results from the Cypher statement as Json to the provided file</p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">apoc.export.json.all(file,config)</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>exports whole database as Json to the provided file</p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">apoc.export.json.data(nodes,rels,file,config)</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>exports given nodes and relationships as Json to the provided file</p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">apoc.export.json.graph(graph,file,config)</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>exports given graph object as Json to the provided file</p>
                           </td>
                        </tr>
                     </tbody>
                  </table>
               </div>
            </div>
            <div class="table" id="d0e8002">
               <table class="table" summary="Config" border="1">
                  <caption class="table-title">Table&nbsp;7.3.&nbsp;Config</caption>
                  <colgroup>
                     <col class="col_1"></col>
                     <col class="col_2"></col>
                  </colgroup>
                  <tbody>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>writeNodeProperties</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>true/false, if true export properties too.</p>
                        </td>
                     </tr>
                  </tbody>
               </table>
            </div>
            <div class="admonitionblock note">
               <table>
                  <tbody>
                     <tr>
                        <td class="icon"><i class="fa icon-note" title="note"></i></td>
                        <td class="content">
                           <p>The labels exported are ordered alphabetically.
                              The output of <code class="literal">labels()</code> function is not sorted, use it in combination with <code class="literal">apoc.coll.sort()</code>.
                           </p>
                        </td>
                     </tr>
                  </tbody>
               </table>
            </div>
            <section class="section" id="_examples_2">
               <div class="titlepage">
                  <div>
                     <div>
                        <h3 class="title"><a class="anchor" href="#_examples_2"></a>7.8.1.&nbsp;Examples
                        </h3>
                     </div>
                  </div>
               </div>
               <p>For the examples below we create the following dataset:</p><pre class="programlisting highlight"><code data-lang="cypher">CREATE (f:User {name:'Adam',age:42,male:true,kids:['Sam','Anna','Grace'], born:localdatetime('2015185T19:32:24'), place:point({latitude: 13.1, longitude: 33.46789})})-[:KNOWS {since: 1993}]-&gt;(b:User {name:'Jim',age:42}),(c:User {age:12})</code></pre><p><span class="formalpara-title">Export query.&nbsp;</span>
                  
               </p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.export.json.query("MATCH (u:User) WHERE age &gt; {age} return u","/tmp/query.json",{params:{age:10}})</code></pre><p>
                  
               </p>
               <p>result:</p><pre class="programlisting highlight"><code data-lang="json">{"u":{"type":"node","id":"0","labels":["User"],"properties":{"born":"2015-07-04T19:32:24","name":"Adam","place":{"crs":"wgs-84","latitude":33.46789,"longitude":13.1,"height":null},"age":42,"male":true,"kids":["Sam","Anna","Grace"]}}}
{"u":{"type":"node","id":"1","labels":["User"],"properties":{"name":"Jim","age":42}}}
{"u":{"type":"node","id":"2","labels":["User"],"properties":{"age":12}}}</code></pre><p><span class="formalpara-title">Export query Complex.&nbsp;</span>
                  
               </p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.export.json.query("RETURN {value:1, data:[10,'car',null, point({ longitude: 56.7, latitude: 12.78 }), point({ longitude: 56.7, latitude: 12.78, height: 8 }), point({ x: 2.3, y: 4.5 }), point({ x: 2.3, y: 4.5, z: 2 }),date('2018-10-10'), datetime('2018-10-18T14:21:40.004Z'), localdatetime({ year:1984, week:10, dayOfWeek:3, hour:12, minute:31, second:14, millisecond: 645 }), {x:1, y:[1,2,3,{age:10}]}]} as key","/tmp/complex.json")</code></pre><p>
                  
               </p>
               <p>result:</p><pre class="programlisting highlight"><code data-lang="json">{"key":{"data":[10,"car",null,{"crs":"wgs-84","latitude":56.7,"longitude":12.78,"height":null},{"crs":"wgs-84-3d","latitude":56.7,"longitude":12.78,"height":8.0},{"crs":"cartesian","x":2.3,"y":4.5,"z":null},{"crs":"cartesian-3d","x":2.3,"y":4.5,"z":2.0},"2018-10-10","2018-10-18T14:21:40.004Z","1984-03-07T12:31:14.645",{"x":1,"y":[1,2,3,{"age":10}]}],"value":1}}</code></pre><p><span class="formalpara-title">Export queryList.&nbsp;</span>
                  
               </p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.export.json.query("MATCH (u:User) RETURN COLLECT(u) as list","/tmp/list.json",{params:{age:10}})</code></pre><p>
                  
               </p>
               <p>result:</p><pre class="programlisting highlight"><code data-lang="json">{"list":[{"type":"node","id":"0","labels":["User"],"properties":{"born":"2015-07-04T19:32:24","name":"Adam","place":{"crs":"wgs-84","latitude":33.46789,"longitude":13.1,"height":null},"age":42,"male":true,"kids":["Sam","Anna","Grace"]}},{"type":"node","id":"1","labels":["User"],"properties":{"name":"Jim","age":42}},{"type":"node","id":"2","labels":["User"],"properties":{"age":12}}]}</code></pre><p><span class="formalpara-title">Export queryMap.&nbsp;</span>
                  
               </p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.export.json.query("MATCH (u:User)-[r:KNOWS]-&gt;(d:User) RETURN u {.*}, d {.*}, r {.*}","/tmp/map.json",{params:{age:10}})</code></pre><p>
                  
               </p>
               <p>result:</p><pre class="programlisting highlight"><code data-lang="json">{"u":{"born":"2015-07-04T19:32:24","name":"Adam","place":{"crs":"wgs-84","latitude":33.46789,"longitude":13.1,"height":null},"age":42,"male":true,"kids":["Sam","Anna","Grace"]},"d":{"name":"Jim","age":42},"r":{"since":1993}}</code></pre><p><span class="formalpara-title">Export all.&nbsp;</span>
                  
               </p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.export.json.all("/tmp/all.json",{useTypes:true})</code></pre><p>
                  
               </p>
               <p>result:</p><pre class="programlisting highlight"><code data-lang="json">{"type":"node","id":"0","labels":["User"],"properties":{"born":"2015-07-04T19:32:24","name":"Adam","place":{"crs":"wgs-84","latitude":33.46789,"longitude":13.1,"height":null},"age":42,"male":true,"kids":["Sam","Anna","Grace"]}}
{"type":"node","id":"1","labels":["User"],"properties":{"name":"Jim","age":42}}
{"type":"node","id":"2","labels":["User"],"properties":{"age":12}}
{"id":"0","type":"relationship","label":"KNOWS","properties":{"since":1993},"start":{"id":"0","labels":["User"]},"end":{"id":"1","labels":["User"]}}</code></pre><p><span class="formalpara-title">Export graph.&nbsp;</span>
                  
               </p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.graph.fromDB('test',{}) yield graph
CALL apoc.export.json.graph(graph,"tmp/graph.json",{})
YIELD nodes, relationships, properties, file, source,format, time
RETURN *</code></pre><p>
                  
               </p>
               <p>result:</p><pre class="programlisting highlight"><code data-lang="json">{"type":"node","id":"0","labels":["User"],"properties":{"born":"2015-07-04T19:32:24","name":"Adam","place":{"crs":"wgs-84","latitude":33.46789,"longitude":13.1,"height":null},"age":42,"male":true,"kids":["Sam","Anna","Grace"]}}
{"type":"node","id":"1","labels":["User"],"properties":{"name":"Jim","age":42}}
{"type":"node","id":"2","labels":["User"],"properties":{"age":12}}
{"id":"0","type":"relationship","label":"KNOWS","properties":{"since":1993},"start":{"id":"0","labels":["User"]},"end":{"id":"1","labels":["User"]}}</code></pre><p><span class="formalpara-title">Export data.&nbsp;</span>
                  
               </p><pre class="programlisting highlight"><code data-lang="cypher">MATCH (nod:User)
MATCH ()-[rels:KNOWS]-&gt;()
WITH collect(nod) as a, collect(rels) as b
CALL apoc.export.json.data(a, b, "tmp/data.json", null)
YIELD nodes, relationships, properties, file, source,format, time
RETURN *</code></pre><p>
                  
               </p>
               <p>result:</p><pre class="programlisting highlight"><code data-lang="json">{"type":"node","id":"0","labels":["User"],"properties":{"born":"2015-07-04T19:32:24","name":"Adam","place":{"crs":"wgs-84","latitude":33.46789,"longitude":13.1,"height":null},"age":42,"male":true,"kids":["Sam","Anna","Grace"]}}
{"type":"node","id":"1","labels":["User"],"properties":{"name":"Jim","age":42}}
{"type":"node","id":"2","labels":["User"],"properties":{"age":12}}
{"id":"0","type":"relationship","label":"KNOWS","properties":{"since":1993},"start":{"id":"0","labels":["User"]},"end":{"id":"1","labels":["User"]}}</code></pre><p><span class="formalpara-title">Export query with config <code class="literal">writeNodeProperties</code>.&nbsp;</span>
                  
               </p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.export.json.query("MATCH p = (u:User)-[rel:KNOWS]-&gt;(u2:User) RETURN rel","/tmp/writeNodeProperties.json",{writeNodeProperties:true}})</code></pre><p>
                  
               </p>
               <p>result:</p><pre class="programlisting highlight"><code data-lang="json">{"rel":{"id":"0","type":"relationship","label":"KNOWS","properties":{"since":1993},"start":{"id":"0","labels":["User"],"properties":{"born":"2015-07-04T19:32:24","name":"Adam","place":{"crs":"wgs-84","latitude":33.46789,"longitude":13.1,"height":null},"age":42,"male":true,"kids":["Sam","Anna","Grace"]}},"end":{"id":"1","labels":["User"],"properties":{"name":"Jim","age":42}}}}</code></pre></section>
         </section>
         <section class="section" id="export-cypher">
            <div class="titlepage">
               <div>
                  <div>
                     <h2 class="title" style="clear: both"><a class="anchor" href="#export-cypher"></a>7.9.&nbsp;Export to Cypher Script
                     </h2>
                  </div>
               </div>
            </div>
            <div class="informalexample">
               <p>If you are experimenting with imports that are failing you can add the <code class="literal">--debug</code> command line parameter, to see which statement was executed last and cause the failure.
               </p>
               <p>Also check the memory configuration of your Neo4j instance, you might want to up the HEAP to <span class="strong"><strong>2&#8211;4GB</strong></span> with the <code class="literal">dbms.memory.heap.max_size=2G</code> setting in <code class="literal">neo4j.conf</code>.
               </p>
               <p>And provide more memory to cypher-shell itself by prefixing the command with: <code class="literal">JAVA_OPTS=-Xmx4G bin/cypher-shell&nbsp;&#8230;</code></p>
            </div>
            <p>Make sure to set the config options in your <code class="literal">neo4j.conf</code></p>
            <p><span class="formalpara-title">neo4j.conf.&nbsp;</span>
               
            </p><pre class="screen highlight"><code>apoc.export.file.enabled=true
apoc.import.file.enabled=true</code></pre><p>
               
            </p>
            <p>Data is exported as Cypher statements to the given file.</p>
            <p>It is possible to choose between three export formats:</p>
            <div class="itemizedlist">
               <ul class="itemizedlist" style="list-style-type: disc; ">
                  <li class="listitem"><code class="literal">neo4j-shell</code>: for Neo4j Shell and partly <code class="literal">apoc.cypher.runFile</code></li>
                  <li class="listitem"><code class="literal">cypher-shell</code>: for Cypher shell
                  </li>
                  <li class="listitem"><code class="literal">plain</code>: doesn&#8217;t output begin / commit / await just plain Cypher
                  </li>
               </ul>
            </div>
            <p>You can also use the Optimizations like: <code class="literal">useOptimizations: {config}</code>
               Config could have this params:
            </p>
            <div class="itemizedlist">
               <ul class="itemizedlist" style="list-style-type: disc; ">
                  <li class="listitem"><code class="literal">unwindBatchSize</code>:  (default 100)
                  </li>
                  <li class="listitem"><code class="literal">type</code>: possible values ('NONE', 'UNWIND_BATCH', 'UNWIND_BATCH_PARAMS') (default 'UNWIND_BATCH')
                  </li>
               </ul>
            </div>
            <p>With <code class="literal">NONE</code> it will export the file with <code class="literal">CREATE</code> statement;
            </p>
            <p>With 'UNWIND_BATCH` it will export the file by batching the entities with the <code class="literal">UNWIND</code> method as explained in this
               <a class="link" href="https://medium.com/neo4j/5-tips-tricks-for-fast-batched-updates-of-graph-structures-with-neo4j-and-cypher-73c7f693c8cc" target="_top">article</a>.
            </p>
            <p>To change the export format, you have to set it on the config params like <code class="literal">{format : "cypher-shell"}</code>.
            </p>
            <p>By default the format is <code class="literal">neo4j-shell</code>.
            </p>
            <p>If you want to export to separate files, e.g. to later use the <code class="literal">apoc.cypher.runFiles/runSchemaFiles</code> procedures, you can add <code class="literal">separateFiles:true</code>.
            </p>
            <p>It is possible to choose between four cypher update operation types:
               To change the cypher update operation, you have to set it on the config params like <code class="literal">{cypherFormat: "updateAll"}</code></p>
            <div class="itemizedlist">
               <ul class="itemizedlist" style="list-style-type: disc; ">
                  <li class="listitem"><code class="literal">create</code>: all CREATE
                  </li>
                  <li class="listitem"><code class="literal">updateAll</code>: MERGE instead of CREATE
                  </li>
                  <li class="listitem"><code class="literal">addStructure</code>: MATCH for nodes + MERGE for rels
                  </li>
                  <li class="listitem"><code class="literal">updateStructure</code>: MERGE + MATCH for nodes and rels
                  </li>
               </ul>
            </div>
            <p>Format and cypherFormat can be used both in the same query giving you complete control over the exact export format:</p><pre class="programlisting highlight"><code data-lang="cypher">call apoc.export.cypher.query(
"MATCH (p1:Person)-[r:KNOWS]-&gt;(p2:Person) RETURN p1,r,p2",
"/tmp/friendships.cypher",
{format:'plain',cypherFormat:'updateStructure'})`</code></pre><p><code class="literal">YIELD file, source, format, nodes, relationships, properties, time</code></p>
            <div class="informaltable">
               <div class="table" id="d0e8270">
                  <table class="informaltable" border="1">
                     <colgroup>
                        <col class="col_1"></col>
                        <col class="col_2"></col>
                     </colgroup>
                     <tbody>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">apoc.export.cypher.all(file,config)</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>exports whole database incl. indexes as Cypher statements to the provided file</p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">apoc.export.cypher.data(nodes,rels,file,config)</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>exports given nodes and relationships incl. indexes as Cypher statements to the provided file</p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">apoc.export.cypher.graph(graph,file,config)</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>exports given graph object incl. indexes as Cypher statements to the provided file</p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">apoc.export.cypher.query(query,file,config)</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>exports nodes and relationships from the Cypher statement incl. indexes as Cypher statements to the provided file</p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">apoc.export.cypher.schema(file,config)</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>exports all schema indexes and constraints to cypher</p>
                           </td>
                        </tr>
                     </tbody>
                  </table>
               </div>
            </div>
            <div class="admonitionblock note">
               <table>
                  <tbody>
                     <tr>
                        <td class="icon"><i class="fa icon-note" title="note"></i></td>
                        <td class="content">
                           <p>The labels exported are ordered alphabetically.
                              The output of <code class="literal">labels()</code> function is not sorted, use it in combination with <code class="literal">apoc.coll.sort()</code>.
                           </p>
                        </td>
                     </tr>
                  </tbody>
               </table>
            </div>
            <section class="section" id="_roundtrip_example">
               <div class="titlepage">
                  <div>
                     <div>
                        <h3 class="title"><a class="anchor" href="#_roundtrip_example"></a>7.9.1.&nbsp;Roundtrip Example
                        </h3>
                     </div>
                  </div>
               </div>
               <p>You can use this roundtrip example e.g. on the <code class="literal">:play movies</code> movie graph.
               </p>
               <p>Make sure to set the config options in your <code class="literal">neo4j.conf</code></p>
               <p><span class="formalpara-title">neo4j.conf.&nbsp;</span>
                  
               </p><pre class="screen highlight"><code>apoc.export.file.enabled=true
apoc.import.file.enabled=true</code></pre><p>
                  
               </p>
               <p>Export the data in plain format and multiple files:</p><pre class="programlisting highlight"><code data-lang="cypher">call apoc.export.cypher.query("match (n)-[r]-&gt;(n2) return * limit 100",
 "/tmp/mysubset.cypher",
 {format:'plain',separateFiles:true});</code></pre><p>This should result in 4 files in your directory.</p><pre class="programlisting highlight"><code data-lang="shell">ls -1 /tmp/mysubset.*
/tmp/mysubset.cleanup.cypher
/tmp/mysubset.nodes.cypher
/tmp/mysubset.relationships.cypher
/tmp/mysubset.schema.cypher</code></pre><p>Import the data in 4 steps, first the schema, then nodes and relationships, then cleanup.</p>
               <p></p>
               <p>call apoc.cypher.runSchemaFile('/tmp/mysubset.schema.cypher');
                  call apoc.cypher.runFiles(['/tmp/mysubset.nodes.cypher','/tmp/mysubset.relationships.cypher']);
               </p>
               <p>call apoc.cypher.runFile('/tmp/mysubset.cleanup.cypher');
                  call apoc.cypher.runSchemaFile('/tmp/mysubset.cleanup.cypher');
                  ---
               </p>
               <p>The <code class="literal">run*</code> procedures have some optional config:
               </p>
               <div class="itemizedlist">
                  <ul class="itemizedlist" style="list-style-type: disc; ">
                     <li class="listitem"><code class="literal">{statistics:true/false}</code> to output a row of update-stats per statement, default is true
                     </li>
                     <li class="listitem"><code class="literal">{timeout:1 or 10}</code> for how long the stream waits for new data, default is 10
                     </li>
                  </ul>
               </div>
            </section>
            <section class="section" id="_stream_back_exported_cypher_script_as_columns">
               <div class="titlepage">
                  <div>
                     <div>
                        <h3 class="title"><a class="anchor" href="#_stream_back_exported_cypher_script_as_columns"></a>7.9.2.&nbsp;Stream back Exported Cypher Script as columns
                        </h3>
                     </div>
                  </div>
               </div>
               <p>If you leave off the file-name as <code class="literal">null</code> the export will instead be streamed back.
               </p>
               <p>In general there will be a <code class="literal">cypherStatements</code> column with the script.
               </p>
               <p>If you use multi-file-splitting as configuration parameter, there will be extra columns with content for</p>
               <div class="itemizedlist">
                  <ul class="itemizedlist" style="list-style-type: disc; ">
                     <li class="listitem">nodeStatements</li>
                     <li class="listitem">relationshipStatements</li>
                     <li class="listitem">cleanupStatements</li>
                     <li class="listitem">schemaStatements</li>
                  </ul>
               </div>
               <p>If you also specify the <code class="literal">streamStatements:true</code> then each batch (by <code class="literal">batchSize</code> which defaults to 10k) of statements will be returned as a row.
                  So you can use your client to reconstruct the cypher script.
               </p>
               <p><span class="formalpara-title">Simple Example for Streaming.&nbsp;</span>
                  
               </p><pre class="programlisting highlight"><code data-lang="cypher">echo "
CALL apoc.export.cypher.all(null,{streamStatements:true,batchSize:100}) YIELD cypherStatements RETURN cypherStatements;
" | ./bin/cypher-shell --non-interactive --format plain</code></pre><p>
                  
               </p>
            </section>
            <section class="section" id="_examples_3">
               <div class="titlepage">
                  <div>
                     <div>
                        <h3 class="title"><a class="anchor" href="#_examples_3"></a>7.9.3.&nbsp;Examples
                        </h3>
                     </div>
                  </div>
               </div>
               <section class="section" id="_old_method">
                  <div class="titlepage">
                     <div>
                        <div>
                           <h4 class="title"><a class="anchor" href="#_old_method"></a>7.9.3.1.&nbsp;Old method:
                           </h4>
                        </div>
                     </div>
                  </div>
                  <p><span class="formalpara-title">exportAll (neo4j-shell format).&nbsp;</span>Without the optimizations
                  </p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.export.cypher.all({fileName},{config})</code></pre><p>Result:</p><pre class="programlisting highlight"><code data-lang="cypher">begin
CREATE (:Foo:`UNIQUE IMPORT LABEL` {name:"foo", `UNIQUE IMPORT ID`:0});
CREATE (:Bar {name:"bar", age:42});
CREATE (:Bar:`UNIQUE IMPORT LABEL` {age:12, `UNIQUE IMPORT ID`:2});
commit
begin
CREATE INDEX ON :Foo(name);
CREATE CONSTRAINT ON (node:Bar) ASSERT node.name IS UNIQUE;
CREATE CONSTRAINT ON (node:`UNIQUE IMPORT LABEL`) ASSERT node.`UNIQUE IMPORT ID` IS UNIQUE;
commit
schema await
begin
MATCH (n1:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`:0}), (n2:Bar{name:"bar"}) CREATE (n1)-[:KNOWS]-&gt;(n2);
commit
begin
MATCH (n:`UNIQUE IMPORT LABEL`)  WITH n LIMIT 20000 REMOVE n:`UNIQUE IMPORT LABEL` REMOVE n.`UNIQUE IMPORT ID`;
commit
begin
DROP CONSTRAINT ON (node:`UNIQUE IMPORT LABEL`) ASSERT node.`UNIQUE IMPORT ID` IS UNIQUE;
commit</code></pre><p><span class="formalpara-title">exportSchema (neo4j-shell format).&nbsp;</span>
                     
                  </p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.export.cypher.schema({fileName},{config})</code></pre><p>
                     
                  </p>
                  <p>Result:</p><pre class="programlisting highlight"><code data-lang="cypher">begin
CREATE INDEX ON :Foo(name);
CREATE CONSTRAINT ON (node:Bar) ASSERT node.name IS UNIQUE;
commit
schema await</code></pre></section>
               <section class="section" id="_new_method">
                  <div class="titlepage">
                     <div>
                        <div>
                           <h4 class="title"><a class="anchor" href="#_new_method"></a>7.9.3.2.&nbsp;New method:
                           </h4>
                        </div>
                     </div>
                  </div>
                  <p>With the optimizations</p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.export.cypher.all({fileName},{config})</code></pre><p>Result:</p><pre class="programlisting highlight"><code data-lang="cypher">BEGIN
CREATE INDEX ON :Bar(first_name,last_name);
CREATE INDEX ON :Foo(name);
CREATE CONSTRAINT ON (node:Bar) ASSERT node.name IS UNIQUE;
CREATE CONSTRAINT ON (node:`UNIQUE IMPORT LABEL`) ASSERT node.`UNIQUE IMPORT ID` IS UNIQUE;
COMMIT
SCHEMA AWAIT
BEGIN
UNWIND [{_id:3, properties:{age:12}}] as row
CREATE (n:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row._id}) SET n += row.properties SET n:Bar;
UNWIND [{_id:2, properties:{age:12}}] as row
CREATE (n:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row._id}) SET n += row.properties SET n:Bar:Person;
UNWIND [{_id:0, properties:{born:date('2018-10-31'), name:"foo"}}, {_id:4, properties:{born:date('2017-09-29'), name:"foo2"}}] as row
CREATE (n:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row._id}) SET n += row.properties SET n:Foo;
UNWIND [{name:"bar", properties:{age:42}}, {name:"bar2", properties:{age:44}}] as row
CREATE (n:Bar{name: row.name}) SET n += row.properties;
UNWIND [{_id:6, properties:{age:99}}] as row
CREATE (n:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row._id}) SET n += row.properties;
COMMIT
BEGIN
UNWIND [{start: {_id:0}, end: {name:"bar"}, properties:{since:2016}}, {start: {_id:4}, end: {name:"bar2"}, properties:{since:2015}}] as row
MATCH (start:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row.start._id})
MATCH (end:Bar{name: row.end.name})
CREATE (start)-[r:KNOWS]-&gt;(end) SET r += row.properties;
COMMIT
BEGIN
MATCH (n:`UNIQUE IMPORT LABEL`)  WITH n LIMIT 20000 REMOVE n:`UNIQUE IMPORT LABEL` REMOVE n.`UNIQUE IMPORT ID`;
COMMIT
BEGIN
DROP CONSTRAINT ON (node:`UNIQUE IMPORT LABEL`) ASSERT (node.`UNIQUE IMPORT ID`) IS UNIQUE;
COMMIT</code></pre></section>
            </section>
         </section>
      </section>
      <footer><script type="text/javascript">
          (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
          })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
          //Allow Linker
          ga('create', 'UA-1192232-34','auto', {'allowLinker': true});
          ga('send', 'pageview');
          // Load the plugin.
          ga('require', 'linker');
          // Define which domains to autoLink.
          ga('linker:autoLink', ['neo4j.org','neo4j.com','neotechnology.com','graphdatabases.com','graphconnect.com']);
        </script><script type="text/javascript">
          document.write(unescape("%3Cscript src='//munchkin.marketo.net/munchkin.js' type='text/javascript'%3E%3C/script%3E"));
        </script><script>Munchkin.init('773-GON-065');</script></footer>
   </body>
</html>